﻿using SupportLayer.Constantes.Tabelas;
using SupportLayer.Objetos.Usuario;
using System;
using System.Data;
using System.Collections.Generic;
using SupportLayer.Constantes.Usuario;

namespace DataLayer
{
  public class AcessoBanco
  {
    private DataSet ds = new DataSet();

    #region Selects

    public UsuarioClass SelectUser(int ID)
    {
      try
      {
        return new Connection().Select(ID.toSelectUserQueryString()).toUserClass()[0];
      }
      catch (Exception ex)
      {
        if (ex.Message.Contains("reference not set") || ex.Message.Contains("objeto não definida"))
          return null;

        throw;
      }
    }
    public List<UsuarioClass> GetNotConfirmedMailUsers()
    {
      string query_string = "select * from " + TABELAS.USUARIO +
        " where " + USUARIO.CONFIRMOU_EMAIL + "='N'";

      return new Connection().Select(query_string).toUserClass();
    }

    public UsuarioClass Login(string Login, string Senha)
    {
      try
      {
        return new Connection().Select(Login.toSelectUserQueryString(Senha)).toUserClass()[0];
      }
      catch (Exception ex)
      {
        if (ex.Message.Contains("reference not set") || ex.Message.Contains("objeto não definida"))
          return null;

        throw;
      }
    }
    public LogClass SelectLog(DateTime dt, int UsuarioID)
    {
      try
      {
        var i = new Connection().Select(dt.toSelectLogQueryString(UsuarioID)).toLogClass();
        if (i == null || i.Count == 0)
          return null;
        return i[0];
      }
      catch (Exception)
      {
        throw;
      }
    }

    #endregion Selects

    #region Inserts

    public Int64 InsertUser(UsuarioClass User)
    {
      try
      {
        return new Connection().Insert(User.toInsertQueryString(TABELAS.USUARIO));
      }
      catch (Exception)
      {
        throw;
      }
    }

    public Int64 InsertLog(LogClass log)
    {
      try
      {
        return new Connection().Insert(log.toInsertQueryString(TABELAS.LOG));
      }
      catch (Exception)
      {
        throw;
      }
    }

    #endregion Inserts

    #region Gets
    public List<LogClass> GetLogs(int _userID)
    {
      try
      {
        return new Connection().Select("select * from " + TABELAS.LOG +
          " where " + LOG.USUARIO_ID + " = " + _userID).toLogClass();
      }
      catch (Exception)
      {
        throw;
      }
    }
    #endregion

    #region Updates
    public void UpdateMailUsuario(UsuarioClass Usuario)
    {
      try
      {
        new Connection().Update(Usuario.updateMailQueryString());
      }
      catch (Exception)
      {
        throw;
      }
    }
    public void UpdateLog(LogClass log)
    {
      try
      {
        new Connection().Update(log.toUpdateQueryString());
      }
      catch (Exception)
      {
        throw;
      }
    }
    #endregion

    #region Checks
    public bool CheckEmail(string email)
    {
      try
      {
        DataSet ds = new Connection().Select(email.checkMailQueryString());

        if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
          return true;

        return false;
      }
      catch (Exception)
      {
        throw;
      }
    }
    public bool CheckUserName(string userName)
    {
      try
      {
        DataSet ds = new Connection().Select(userName.checkUsernameQueryString());
        if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
          return true;

        return false;
      }
      catch (Exception)
      {
        throw;
      }

    }

    #endregion
  }
}